<?php
namespace app\yxwadmin\controller;
use app\yxwadmin\controller\Admin;
use think\Controller;
use think\Cache;

/**
* 后台框架
*/
class Index extends Admin{

	public function index(){
        $uname = session('uname');
        $uid = session('uid');
        $user = model('UserModel')
        ->alias('a')
        ->field('a.id,a.user_name,b.userid,b.roleid,c.role_name')
        ->where('a.id',$uid)
        ->join('Roleinusers b','b.userid=a.id','LEFT')
        ->join('Role c','c.id=b.roleid')
        ->group('a.id')
        ->find();

        $last_login_info = session('last_login_info');
        
        //获取缓存大小
        $cacheSize = 0;
        if (is_dir(CACHE_PATH)) {
            $cacheSize = $this->getDirSize(CACHE_PATH);
        }
        //获取临时文件大小
        $tempSize = 0;
        if (is_dir(TEMP_PATH)) {
            $tempSize = $this->getDirSize(TEMP_PATH);
        }
        $data['ctsize'] = $this->getRealSize($cacheSize+$tempSize);

        //$sysset = model('SetModel')->find();
        $data['sysname'] = "美文音乐相册管理";//$sysset['sysname'];
        $data['copyright'] = "";//$sysset['copyright'];                
        
        //加载权限码
        if(!Cache("permissioncodes_".$uid)){
            $is_manager = model('RoleInUsersModel')->alias('a')->join("Role b","a.roleid=b.id")->where('is_manager','1')->where('a.userid',$uid)->count();
            if($is_manager>0){
                $permissioncodes = model("PermissionModel")->field('permission_code')
                ->where("product_code","photo_albums_bless")
                ->where("permission_type","Leaf")->order("sort_index")->column("permission_code");
            }else{
                $permissioncodes = model('RoleInUsersModel')->distinct(true)->alias('b')->field('d.permission_code')
                ->join("Permissioninrole c","b.roleid=c.roleid")
                ->join("Permission d","d.permission_code=c.permission_code")
                ->where("permission_type","Leaf")
                ->where("b.userid",$uid)
                ->order("sort_index")
                ->column("d.permission_code");
            }
            Cache("permissioncodes_".$uid,$permissioncodes);
        }


        $this->assign('data', $data);
        $this->assign('user', $user);
        $this->assign('last_login_info', $last_login_info);
        $this->get_menu_html($uid);
        return $this->view->fetch('index/index');
    }

    private function get_menu_html($uid){
        $is_manager = model('RoleInUsersModel')->alias('a')->join("Role b","a.roleid=b.id")->where('is_manager','1')->where('a.userid',$uid)->count();
        $menu_html = '';        
        
        if($is_manager>0){
            //管理员菜单权限
            $topmenus = model("PermissionModel")->field('permission_code,menu_name,action,icon')
            ->where('is_hidden',0)->where("permission_type","Menu")
            ->where("product_code","photo_albums_bless")
            ->where("parent_code","")->order("sort_index")->select();                    

            $frist_menu = model("PermissionModel")->field('permission_code,parent_code,menu_name,action,controller,target')->where('is_hidden',0)->where("permission_type","Menu")
            ->where("parent_code",$topmenus[0]['permission_code'])->order("sort_index")->find();            
            $frist_menu['url'] = url($frist_menu['controller']."/".$frist_menu['action']);
            
            foreach($topmenus as $topmenu){
                $menu_html = $menu_html.'<li class="'.$topmenu['action'].'"><a href="#"><i class="'.$topmenu['icon'].'"></i><span class="nav-label">'.$topmenu['menu_name'].'</span><span class="fa arrow""></span></a>';
                $childmenus = model("PermissionModel")->field('permission_code,parent_code,menu_name,action,controller,target')->where('is_hidden',0)->where("permission_type","Menu")
                ->where("parent_code",$topmenu['permission_code'])->order("sort_index")->select();
                $menu_html = $menu_html.'<ul class="nav nav-second-level">';

                foreach($childmenus as $childmenu){
                    $menu_html = $menu_html.'<li><a class="J_menuItem" href="'.url($childmenu['controller']."/".$childmenu['action']).'">'.$childmenu['menu_name'].'</a></li>';
                }                
                $menu_html = $menu_html.'</ul></li>';
            }
        }else{
            //获取用户菜单权限
            $topmenus = model('RoleInUsersModel')->distinct(true)->alias('b')->field('d.permission_code,d.parent_code,d.menu_name,d.action,d.controller,d.target,d.icon')
            ->join("Permissioninrole c","b.roleid=c.roleid")
            ->join("Permission d","d.permission_code=c.permission_code")
            ->where("product_code","photo_albums_bless")
            ->where("permission_type","Menu")
            ->where("b.userid",$uid)
            ->where("d.parent_code","")
            ->order("sort_index")
            ->select();
            $frist_menu = model("PermissionModel")->field('permission_code,parent_code,menu_name,action,controller,target')->where('is_hidden',0)->where("permission_type","Menu")
            ->where("parent_code",$topmenus[0]['permission_code'])->order("sort_index")->find();
            $frist_menu['url'] = url($frist_menu['controller']."/".$frist_menu['action']);
            foreach($topmenus as $topmenu){
                $menu_html = $menu_html.'<li><a href="#"><i class="'.$topmenu['icon'].'"></i><span class="nav-label">'.$topmenu['menu_name'].'</span><span class="fa arrow"></span></a>';
                $childmenus = model('RoleInUsersModel')->distinct(true)->alias('b')->field('d.permission_code,d.parent_code,d.menu_name,d.action,d.controller,d.target,d.icon')
                ->join("Permissioninrole c","b.roleid=c.roleid")
                ->join("Permission d","d.permission_code=c.permission_code")
                ->where("product_code","photo_albums_bless")
                ->where("permission_type","Menu")
                ->where("b.userid",$uid)
                ->where("d.parent_code",$topmenu['permission_code'])
                ->order("sort_index")
                ->select(); 
                $menu_html = $menu_html.'<ul class="nav nav-second-level">';

                foreach($childmenus as $childmenu){
                    $menu_html = $menu_html.'<li><a class="J_menuItem" href="'.url($childmenu['controller']."/".$childmenu['action']).'">'.$childmenu['menu_name'].'</a></li>';
                }                
                $menu_html = $menu_html.'</ul></li>';
            }
        }
        $this->assign('frist_menu',$frist_menu);
        $this->assign('menu_html', $menu_html);
    }

    /**
     * 清除缓存
     */
    public function clean_temp_cache(){
    	Cache::clear();
    	array_map( 'unlink', glob( TEMP_PATH.DS.'*.php' ) );
    	$this->success( '清除成功', 'index/index');
    }

    /**
     * 获取文件夹大小
     */
    protected function getDirSize($dir){
        $sizeResult = 0;
        $handle = opendir($dir);
        while (false!==($FolderOrFile = readdir($handle))) { 
            if($FolderOrFile != "." && $FolderOrFile != "..") { 
                if(is_dir("$dir/$FolderOrFile")) { 
                    $sizeResult += $this->getDirSize("$dir/$FolderOrFile"); 
                }
                else { 
                    $sizeResult += filesize("$dir/$FolderOrFile"); 
                }
            } 
        }
        closedir($handle);

        return $sizeResult;
    }

    /**
     * 单位自动转换函数
     */
    protected function getRealSize($size){ 
        $kb = 1024;   // Kilobyte
        $mb = 1024 * $kb; // Megabyte
        $gb = 1024 * $mb; // Gigabyte
        $tb = 1024 * $gb; // Terabyte
        if($size < $kb) { 
            return $size." B";
        }
        else if($size < $mb) { 
            return round($size/$kb,2)." KB";
        }
        else if($size < $gb) { 
            return round($size/$mb,2)." MB";
        }
        else if($size < $tb) { 
            return round($size/$gb,2)." GB";
        }
        else { 
            return round($size/$tb,2)." TB";
        }
    }
}